package com.zjy.org.controller;

import com.zjy.basic.annotation.PreAuthorize;
import com.zjy.basic.util.AjaxResult;
import com.zjy.basic.util.PageList;
import com.zjy.org.domain.Department;
import com.zjy.org.query.DepartmentQuery;
import com.zjy.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 查询单个
     * @param id
     * @return
     */
    //GET http://localhost:8080/department/1
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个" )
    @PreAuthorize(name = "查询单个部门",sn="department:getOne")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    //GET http://localhost:8080/department
    @GetMapping
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(name = "查询所有部门",sn="department:getAll")
    public List<Department> findAll(){
        return departmentService.findAll();
    }

    /**
     * 接口：分页查询
     * @param departmentQuery
     * @return
     */
    //post http://localhost:8080/department 对象
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "查询部门列表",sn="department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    //DELETE http://localhost:8080/department/1
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个" )
    @PreAuthorize(name = "删除部门",sn="department:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 新增和修改
     * @param department
     * @return
     */
    //PUT http://localhost:8080/department 对象
    @PutMapping
    @ApiOperation(value = "新增或修改" )
    @PreAuthorize(name = "添加和修改部门",sn="department:save")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.add(department);
            }
            departmentService.update(department);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    //PATCH http://localhost:8080/department/1
    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(name = "批量删除部门",sn="department:patchDel")
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            departmentService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(name = "查询部门树",sn="department:deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
